$(function () {
    findCuisineAll();
});

function findCuisineAll() {
    let data = null;
    $.ajax({
        url: '/back/cuisine/findAll',
        data: {isShow: 1},
        type: 'get',
        dataType: 'json',
        success: function (res) {
            let data = res.data;
            setCuisineData(data);
        }
    });
}


function setCuisineData(data) {
    layui.use(['form'],
        function () {
            let form = layui.form;
            let html = '<option value=""></option>';
            for (let i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
            }
            $("#cuisine").html(html);
            form.render();//重新渲染
        });
}

let imgHref = "";
//加载模块
layui.use(['layedit', 'upload', 'element', 'layer', 'form', 'table'], function () { //亦可加载特定模块：layui.use(['layer', 'laydate', function(){
    //得到各种内置组件
    var layer = layui.layer //弹层
        , form = layui.form
        , upload = layui.upload
        , element = layui.element;
    let layedit = layui.layedit;

    layedit.build('content', {
        height: 180 //设置编辑器高度
    });
    layedit.set({
        uploadImage: {
            url: '/upload' //接口url
            , type: '' //默认post
        }
    });
    let index = layedit.build('content'); //建立编辑器
    //常规使用 - 普通图片上传
    var uploadInst = upload.render({
        elem: '#test1',
        url: '/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });

        },
        done: function (res) {
            //如果上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            imgHref = res.data.src;

        }

    });
    //监听提交
    form.on('submit(add)',
        function (data) {
            // console.log(data.field)
            data = data.field;
            data.imgHref = imgHref;
            data.content = layedit.getContent(index);
            console.log(data);
            let res = myAjax("/back/product/add", data, 'post');
            if (res.count > 0) {
                layer.alert("增加成功", {
                        icon: 6
                    },
                    function () {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
            } else {
                layer.alert("增加失败");
            }
            return false;
        });

});
